<?php

class ctrl_wish_favorites extends L {

    /**
     * 描述 ：产品收藏列表页面展示
     * 作者 ：Zoro.Zhu
    **/
    public function index(){
        $this->view->_pagingHtml = $this->paging();
        $this->view->headTitle = '产品收藏列表';
        $this->display('/html/wish/favorites.html');
    }

    /**
     * 描述 : 获取分页
     * 作者 : Zoro.Zhu
     */
    public function &paging($params = array()){
        $sql = model_wishFavorites::searchSql($params);
        $config = array( 
            '产品ID' => array(
                '_attr' => array(
                    'body' => '{`productId`}',
                    'sort' => 'productId'
                )
            ),
            '已收藏情况' => '{`_ok`}',
            '正在收藏情况' => '{`_no`}',
            '添加时间' => array(
                '_attr' => array(
                    'body' => '{`addDate`}',
                    'sort' => 'addDate'
                )
            ),
            '_attr' => array(
                "attr" => array (
                    "table" => 'id=wishFavoritesList class="table table-hover table-condensed table-striped table-scroll jsHoverDetails textC"',
                    "btr" => "align='center' class='of-paging_item_odd tr_{`productId`}'",
                ),
                'data' => &$sql,
                'params' => &$params,
                'call' => array($this, 'asCall'),
                'method' => __METHOD__,
                'save' => 'auto',
                'empty' => '查无此数据'
            )
        );
        return $this->_com->paging($config);
    }

    /**
     * 描述 : 整理数据
     * 作者 : Zoro.Zhu
     */
    public function asCall(&$data){
        foreach($data['data'] as &$val){
            $json = json_decode($val['data'], true);
            $val['_no'] = '';
            $val['_ok'] = '';
            $sql = "SELECT
                `data` ->> '$.account' AS account, lockTime 
            FROM
                `_of_com_mq` 
            WHERE
                `type` = 'buyAddProduct' 
            AND `data` ->> '$.productId' = '{$val['productId']}'
            ORDER BY lockTime";
            $no = L::sql($sql);
            if(!empty($no)){
                $val['_no'] = '<table style="width: 98%"><tr style="text-align: center;color: #0EC16F"><td>账号</td><td>收藏时间</td></tr>';
                foreach($no as $row){
                    $val['_no'] .= "<tr style=\"text-align: center\"><td>{$row['account']}</td><td>{$row['lockTime']}</td></tr>";
                }
                $val['_no'] .= '</table>';
            }
            if(!empty($json['ok'])){
                $val['_ok'] = '<table style="width: 98%"><tr style="text-align: center;color: #6c4aa1"><td>账号</td><td>收藏时间</td><td>结果</td></tr>';
                foreach($json['ok'] as $row){
                    $val['_ok'] .= "<tr style=\"text-align: center\"><td>{$row['account']}</td><td>{$row['date']}</td><td>{$row['result']}</td></tr>";
                }
                $val['_ok'] .= '</table>';
            }
        }
    }

    /**
     * 描述 : 新增和修改的页面展示
     * 作者 : Zoro.Zhu
     */
    public function wishFavoritesAdd(){
        $accountQty = L::sql("SELECT
            count( 0 ) AS qty 
        FROM
            `wish_buy_account` 
        WHERE
            1");
        $this->view->ids = !empty($_GET['ids']) ? str_replace(',', "\r\n", $_GET['ids']) : '';
        $this->view->accountQty = $accountQty[0]['qty'];
        $this->display('/html/wish/favoritesAdd.html');
    }

    /**
     * 描述 : 添加产品收藏计划
     * 作者 : Zoro.Zhu
     */
    public function addProduct(){
        set_time_limit(0);
        $_POST = removeBlank($_POST);
        $productId = explode("\r\n", $_POST['productId']);
        if(empty($productId)){
            return ['state' => 300, 'info' => '产品ID不能为空'];
        }
        $dayS = empty($_POST['dayS']) ? 0 : $_POST['dayS'];
        $dayE = empty($_POST['dayE']) ? 0 : $_POST['dayE'];
        $type = empty($_POST['type']) ? 1 : $_POST['type'];
        $accountList = model_wishBuyAccount::getParamsInfo(null, ['account']);
        $info = "";
        $addName = core_base::getUser('name');
        $addDate = date('Y-m-d H:i:s');
        foreach($productId as $id){
            if(!empty($id)){
                $info .= " ('{$id}', '[]', '{$addName}', '{$addDate}'),";
                $aList = array_rand($accountList, $_POST['accountQty']);
                $_POST['accountQty'] == 1 && $aList = [0];
                foreach ($aList as $a) {
                    $time = mt_rand(86400 * $dayS, 86400 * $dayE);
                    of_base_com_mq::set(['buyAddProduct', $id . '-' . $accountList[$a]['account'], $time], ['productId' => $id, 'account' => $accountList[$a]['account'], 'type' => $type]);
                }
            }
        }
        $info = trim($info, ',');
        $sql = "INSERT INTO `wish_favorites`(
             `productId`, `data`, `addName`, `addDate`
        ) VALUES {$info}  ON DUPLICATE KEY UPDATE `productId`= VALUES(`productId`)";
        L::sql($sql);
        return ['state' => 200, 'info' => '操作成功，系统在后台处理中'];
    }

}
return true;